import time
from selenium import webdriver


def getCookie(username, passwd):
    wd = webdriver.Chrome('../浏览器驱动/chromedriver.exe')
    # 打开登录页面
    wd.get('https://passport.bilibili.com/login')

    # 输入账号
    login_username = wd.find_element_by_id('login-username')
    login_username.send_keys(username)

    # 输入密码
    login_passwd = wd.find_element_by_id('login-passwd')
    login_passwd.send_keys(passwd)

    # 点击登录按钮
    login = wd.find_element_by_class_name('btn.btn-login')
    login.click()

    # 休眠10秒,手工验证
    time.sleep(10)

    # 提交登录
    geetest_commit_tip = wd.find_element_by_class_name('geetest_commit_tip')
    geetest_commit_tip.click()
    time.sleep(10)

    wd.get('https://bilibili.com/')
    cookies = wd.get_cookies()
    print(cookies)
    path = '../cookie/{}.txt'.format(username)
    with open(path, 'w') as f:
        f.write(str(cookies))

    wd.close()
